/**
 * Copyright (C) 2018-2021
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.distributor.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.distributor.domain.YxDistributor;
import co.yixiang.modules.distributor.service.YxDistributorService;
import co.yixiang.modules.distributor.service.dto.YxDistributorQueryCriteria;
import co.yixiang.modules.distributor.service.mapper.YxDistributorMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author ysl
* @date 2021-11-21
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxDistributorServiceImpl extends BaseServiceImpl<YxDistributorMapper, YxDistributor> implements YxDistributorService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private YxDistributorMapper yxDistributorMapper;



    /**
     * 查询所有的经销商
     * @return List<YxDistributor>
     */
    @Override
    public Map<String,Object> queryAll(YxDistributorQueryCriteria criteria, Pageable pageable){
        getPage(pageable);
        PageInfo<YxDistributor> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxDistributor.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    public List<YxDistributor> queryAll(YxDistributorQueryCriteria criteria) {
        List<YxDistributor> result = baseMapper.selectList(QueryHelpPlus.getPredicate(YxDistributor.class, criteria));
        return result;
    }

    @Override
    public YxDistributor getYxDistributorById(String distributorId) {
        LambdaQueryWrapper<YxDistributor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YxDistributor::getDistributorId, distributorId);
        return this.getOne(wrapper);
    }


}
